import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../components/index.vue'
import Home from '../components/home.vue'
import album from '../components/Personal/album.vue'
import cami from '../components/Personal/cami.vue'
import editpwd from '../components/Data/editpwd.vue'
import details from '../components/Personal/details.vue'
import camis from '../components/Management/camis.vue'
import users from '../components/Management/users.vue'
import home_index from '../components/home/index.vue'
import api from '../components/Api/api.vue'
import view_404 from '../components/404.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: index },
  {
    path: '/home', component: Home, children: [
      {
        path: '/index',
        component: home_index
      },
      {
        path: '/album',
        component: album
      },
      {
        path: '/cami',
        component: cami
      },
      {
        path: '/details',
        component: details
      },
      {
        path: '/editpwd',
        component: editpwd
      },
      {
        path: '/camis',
        component: camis
      },
      {
        path: '/users',
        component: users
      }
      ,
      {
        path: '/api',
        component: api
      }
    ]
  },
  { path: "*", component: view_404 }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
// to from next
// to = 将要访问的路径
// from = 从那个路径跳转而来
// next() = 放行
// next('/login') = 强制跳转的路径
router.beforeEach((to, from, next) => {
  // 判断用户 是否访问 登陆页面 如果是直接放行
  if (to.path == '/') {
    return next()
  }
  // 获取本地token 值
  const tokenStr = window.sessionStorage.getItem("token");
  // 判断值 是否存储 如果不存在 直接跳转到 登陆页面
  if (!tokenStr) {
    return next('/')
  }
  // 放行
  next()
})
export default router
